import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BomManager } from "@/api/backend/index";

export const useBomM = defineStore(Names.BomManager, {
  state: () => {
    return {
      options: {},
      originData: {},
      bomInfo: {},
    };
  },
  getters: {},
  actions: {
    //上传物料数据
    getDept() {
      return new Promise((resolve, reject) => {
        _get(BomManager + "/export", {}, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //物料字典导入excel文件
    importExcel(data: any) {
      return new Promise((resolve, reject) => {
        _post(BomManager + "/import", data, "formData", "")
          .then((res) => {
            console.log("导入excel文件", res);
            resolve(res as any);
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    //获取bom原始树型数据
    getBomByRole() {
      return new Promise((resolve, reject) => {
        _get(BomManager + "/list", {}, "json", "")
          .then(async (res: any) => {
            this.originData = res.rows; //原始树形结构数据
            resolve(res.rows);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    //获取bom原始树型去重数据
    getBomUniqeByRole() {
      return new Promise((resolve, reject) => {
        _get(BomManager + "/listUnique", {}, "json", "")
          .then(async (res: any) => {
            this.originData = res.rows; //原始树形结构数据
            resolve(res.rows);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
});
